/**
 * @program: LeetCode
 * @description: LeetCode : 59. 螺旋矩阵 II
 * @author: WXY
 * @create: 2023-01-13 13:07
 * @Version 1.0
 **/
public class Num059_generateMatrix {
    public static int[][] generateMatrix(int n) {
        int loop = 0;
        int[][] ans = new int[n][n];
        int start = 0;
        int count = 1;
        int i, j;
        while (loop++ < n / 2) {
            for (j = start; j < n - loop; j++) {
                ans[start][j] = count++;
            }
            for (i = start; i < n - loop; i++) {
                ans[i][j] = count++;
            }
            for (; j >= loop; j--) {
                ans[i][j] = count++;
            }
            for (; i >= loop; i--) {
                ans[i][j] = count++;
            }
            start++;
        }
        if (n % 2 == 1) {
            ans[start][start] = count;
        }
        return ans;
    }

    public static void main(String[] args) {
        int[][] ints = generateMatrix(3);

    }
}
